/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.dependency;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency;
import com.genia.toolbox.projects.toolbox_basics_project.bean.LanguageContainter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Language;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.LanguageManager;
import com.genia.toolbox.spring.provider.init.bean.SpringInitializable;
import com.genia.toolbox.spring.provider.message.manager.CustomResourceBundleMessageSource;

/**
 * dependency class that handles the link beetween {@link Language} and
 * {@link com.genia.toolbox.basics.i18n.SupportedLanguage}.
 */
public abstract class AbstractLanguageModificationDependency
    extends AbstractPersistableDependency<Language>
    implements SpringInitializable
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(AbstractLanguageModificationDependency.class);

  /**
   * the {@link LanguageManager} to use.
   */
  private LanguageManager languageManager;



  /**
   * The method to be called at the start of the application.
   * 
   * @see com.genia.toolbox.spring.provider.init.bean.SpringInitializable#init()
   */
  @Transactional(readOnly = true)
  public void init()
  {
    try {
      for (Language language : getLanguageManager().getAll()) {
        getMessageSource().addSupportedLanguage(createLanguageContainer(language));
      }
    }
    catch (TechnicalException e) {
      LOGGER.error(e.getMessage(), e);
    }
  }

  /**
   * the {@link CustomResourceBundleMessageSource} to use.
   */
  private CustomResourceBundleMessageSource messageSource;



  /**
   * handle the dependencies after creating an object.
   * 
   * @param object
   *          the object that has been created
   * @see com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency#onCreate(java.lang.Object)
   */
  @Override
  public void onPostCreate(Language object)
  {
    getMessageSource().addSupportedLanguage(createLanguageContainer(object));
  }



  /**
   * create a new {@link LanguageContainter} associated to the {@link Language}
   * given in parameter.
   * 
   * @param language
   *          the {@link Language} to create a container for
   * @return a new {@link LanguageContainter} associated to the {@link Language}
   *         given in parameter
   */
  private LanguageContainter createLanguageContainer(Language language)
  {
    LanguageContainter languageContainter = createLanguageContainter();
    languageContainter.setIdentifier(language.getIdentifier());
    return languageContainter;
  }



  /**
   * handle the dependencies before deleting an object.
   * 
   * @param object
   *          the object that will be deleted when an error occurs
   * @see com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency#onDelete(java.lang.Object)
   */
  @Override
  public void onDelete(Language object)
  {
    getMessageSource().removeSupportedLanguage(createLanguageContainer(object));
  }



  /**
   * handle the dependencies before updating an object.
   * 
   * @param object
   *          the object that will be updated
   * @see com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency#onUpdate(java.lang.Object)
   */
  @Override
  public void onUpdate(Language object)
  {
    onDelete(object);
    onPostCreate(object);
  }



  /**
   * constructor.
   */
  protected AbstractLanguageModificationDependency()
  {
    super(Language.class);
  }



  /**
   * factory for {@link LanguageContainter}.
   * 
   * @return a new {@link LanguageContainter}
   */
  public abstract LanguageContainter createLanguageContainter();



  /**
   * getter for the messageSource property.
   * 
   * @return the messageSource
   */
  public CustomResourceBundleMessageSource getMessageSource()
  {
    return messageSource;
  }



  /**
   * setter for the messageSource property.
   * 
   * @param messageSource
   *          the messageSource to set
   */
  public void setMessageSource(CustomResourceBundleMessageSource messageSource)
  {
    this.messageSource = messageSource;
  }



  /**
   * getter for the languageManager property.
   * 
   * @return the languageManager
   */
  public LanguageManager getLanguageManager()
  {
    return languageManager;
  }



  /**
   * setter for the languageManager property.
   * 
   * @param languageManager
   *          the languageManager to set
   */
  public void setLanguageManager(LanguageManager languageManager)
  {
    this.languageManager = languageManager;
  }
}
